package com.syc.legou.search.service.impl;

import com.syc.legou.search.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.data.solr.core.query.*;
import org.springframework.data.solr.core.query.result.FacetAndHighlightPage;
import org.springframework.data.solr.core.query.result.FacetFieldEntry;
import org.springframework.data.solr.core.query.result.HighlightEntry;
import org.springframework.stereotype.Service;
import com.syc.legou.legou_search_api.entity.Vod;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SearchServiceImpl implements SearchService {
    @Autowired
    private SolrTemplate solrTemplate;

    @Override
    public Map<String, Object> search(Map<String, String> searchMap) {
        //1.根据关键字进行搜索
        //1.1获取关键字
        String keywords = searchMap.get("keywords");//前端传递关键字参数名
        //1.2根据关键字构造搜索条件
        FacetAndHighlightQuery query = new SimpleFacetAndHighlightQuery(new Criteria("vod_name").is(keywords));
        //1.3执行查询
        //2.基于影视分类进行facet查询
        //2.1设置facet相关参数
        FacetOptions facetOptions = new FacetOptions();
        facetOptions.addFacetOnField("vod_type");
        query.setFacetOptions(facetOptions);
        //3.品牌过滤
        //3.1获取前端传递的品牌条件
        //3.2判读前端是否传递了品牌条件
        String type = searchMap.get("type");
        if (!StringUtils.isEmpty(type)) {
            //3.3构建过滤条件
            FilterQuery typeFilterQuery = new SimpleFilterQuery(new Criteria("vod_type").is(type));
            query.addFilterQuery(typeFilterQuery);
        }
        //7. 分页
        //7.1 获取分页参数
        String currentPage = searchMap.get("currentPage");
        String pageSize = searchMap.get("pageSize");
        if (StringUtils.isEmpty(currentPage)) {
            currentPage = "1";
        }
        if (StringUtils.isEmpty(pageSize)) {
            pageSize = "10";
        }
        //7.2 设置分页参数
        query.setPageRequest(PageRequest.of(Integer.parseInt(currentPage) - 1, Integer.parseInt(pageSize)));

        //8.高亮查询
        //8.1设置高亮参数
        HighlightOptions highlightOptions = new HighlightOptions();
        //8.1.1设置高亮的域
        highlightOptions.addField("vod_name");
        //8.1.2设置高亮的前缀和后缀
        highlightOptions.setSimplePrefix("<span style='color:rdd'>");
        highlightOptions.setSimplePostfix("</span>");
        query.setHighlightOptions(highlightOptions);

        FacetAndHighlightPage<Vod> page = solrTemplate.queryForFacetAndHighlightPage("collection1", query, Vod.class);
        ///1.4解析查询结果
        List<Vod> content = page.getContent();//当前页数据
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("list", content);
        //2.2解析facet查询结果
        Page<FacetFieldEntry> typeFacetPage = page.getFacetResultPage("vod_type");
        List<FacetFieldEntry> typeContent = typeFacetPage.getContent();
        List<String> typeList = new ArrayList<>();
        for (FacetFieldEntry facetFieldEntry : typeContent) {
            typeList.add(facetFieldEntry.getValue());
        }
        resultMap.put("typeList", typeList);
        //7.3 获取总记录数和总页
        long totalElements = page.getTotalElements();
        int totalPages = page.getTotalPages();
        resultMap.put("total", totalElements);
        resultMap.put("totalPages", totalPages);
        //8.2解析高亮的结果
        for (HighlightEntry<Vod> vodHighlightEntry : page.getHighlighted()) {
            Vod entity = vodHighlightEntry.getEntity();
            List<HighlightEntry.Highlight> highlights = vodHighlightEntry.getHighlights();//高亮的数据
            if (null != highlights && highlights.size() > 0) {
                HighlightEntry.Highlight highlight = highlights.get(0);
                List<String> snipplets = highlight.getSnipplets();
                if (null != snipplets && snipplets.size() > 0) {
                    String name = snipplets.get(0);
                    entity.setVodName(name);
                }
            }
        }


        return resultMap;
    }
}
